//
// Created by 86131 on 2023/10/24.
//

#include <iostream>
using namespace std;
template <typename T>
class ArrayList1 {
private:
    T* data;
    int size;
    int capacity;

public:
    ArrayList1() : data(nullptr), size(0), capacity(0) {}

    ~ArrayList1() {
        delete[] data;
    }

    // 添加元素
    void add(const T& item) {
        if (size == capacity) {
            // 扩展容量
            int newCapacity = capacity == 0 ? 1 : capacity * 2;
            T* newData = new T[newCapacity];
            for (int i = 0; i < size; i++) {
                newData[i] = data[i];
            }
            delete[] data;
            data = newData;
            capacity = newCapacity;
        }
        data[size] = item;
        size++;
    }

    // 获取指定位置的元素
    T& get(int index) {
        if (index >= 0 && index < size) {
            return data[index];
        } else {
            std::cout << "索引越界。" << std::endl;
            // 返回一个默认值，具体处理方式可以根据需求进行修改
            return data[0];
        }
    }

    // 更新指定位置的元素
    void set(int index, const T& item) {
        if (index >= 0 && index < size) {
            data[index] = item;
        } else {
            std::cout << "索引越界。" << std::endl;
        }
    }

    // 删除指定位置的元素
    void remove(int index) {
        if (index >= 0 && index < size) {
            for (int i = index; i < size - 1; i++) {
                data[i] = data[i + 1];
            }
            size--;
        } else {
            std::cout << "索引越界。" << std::endl;
        }
    }

    // 获取线性表的大小
    int getSize() const {
        return size;
    }
    //改变线性表大小
    void setSize(int size1){
        this->size = size1;
    }
    bool empty(){
        return this->size==0;
    }
    int end(){
        return data[size-1];
    }
    int index(int value){
        for (int i = 0; i < size - 1; i++) {
            if(value ==data[i]){return i;}
        }
        cout<<"未找到";
        return 0;
    }
};
//
// Created by 86131 on 2023/11/14.
//
template<typename T>
class MinHeap {
private:
    ArrayList1<T> heap;

    int parent(int index) {
        return (index - 1) / 2;
    }

    int leftChild(int index) {
        return 2 * index + 1;
    }

    int rightChild(int index) {
        return 2 * index + 2;
    }

    void swap(int& a, int& b) {
        int temp = a;
        a = b;
        b = temp;
    }

    void siftUp(int index) {
        while (index > 0 && heap.get(index) < heap.get(parent(index))){
            swap(heap.get(index), heap.get(parent(index)));
            index = parent(index);
        }
    }

    void siftDown(int index) {
        int maxIndex = index;
        int leftIndex = leftChild(index);
        int rightIndex = rightChild(index);

        if (leftIndex < heap.getSize() && heap.get(leftIndex) < heap.get(maxIndex)) {
            maxIndex = leftIndex;
        }

        if (rightIndex < heap.getSize() && heap.get(rightIndex) < heap.get(maxIndex)) {
            maxIndex = rightIndex;
        }

        if (index != maxIndex) {
            swap(heap.get(index), heap.get(maxIndex));
            siftDown(maxIndex);
        }
    }

public:
    void insert(int value) {
        heap.add(value);
        siftUp(heap.getSize() - 1);
    }

    int extractMin() {
        if (heap.empty()) {
            return -1; // or any other appropriate handling for an empty heap
        }

        int max = heap.get(0);
        swap(heap.get(0), heap.get(heap.getSize() - 1));
        heap.remove(heap.getSize()-1);
        siftDown(0);
        return max;
    }
    int getMax(){
        return heap.get(0);
    }
    int getsize(){
        return heap.getSize();
    }

};
//
// Created by 86131 on 2023/11/14.
//

#include <iostream>
using namespace std;
int main()
{
    MinHeap<int> a;
    int n, res = 0;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        int t;
        cin >> t;
        a.insert(t);
    }
    while(a.getsize()>0)
    {
        int x = a.extractMin();
        //a.pop();
        if(a.getsize()==0)
            break;
        int y = a.extractMin();
        x += y;
        res += x;
        a.insert(x);
    }
    cout << res << endl;
    return 0;
}



